package com.yiyun.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yiyun.blog.dao.mapper.ArticleMapper;
import com.yiyun.blog.dao.mapper.CategoryMapper;
import com.yiyun.blog.dao.pojo.Category;
import com.yiyun.blog.service.ArticleService;
import com.yiyun.blog.service.CategoryService;
import com.yiyun.blog.vo.CategoryVo;
import com.yiyun.blog.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Bao
 * @create 2022-03-07-14:58
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public CategoryVo findCategoryById(Long categoryId) {

        Category category = categoryMapper.selectById(categoryId);
        return copy(category);
    }

    @Override
    public Result findAllCategory() {

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Category::getId,Category::getCategoryName);
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        // 交互的对象
        List<CategoryVo> categoryVoList = copyList(categoryList);
        return Result.success(categoryVoList);
    }

    @Override
    public Result findAllCategoryDetail() {

        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<>());
        // 交互的对象
        List<CategoryVo> categoryVoList = copyList(categoryList);
        return Result.success(categoryVoList);
    }

    // 查找该分类下的id
    @Override
    public Result categoryDetailById(Long categoryId) {

        Category category = categoryMapper.selectById(categoryId);
        CategoryVo categoryVo = copy(category);
        return Result.success(categoryVo);
    }

    private List<CategoryVo> copyList(List<Category> categoryList) {

        List<CategoryVo> categoryVoList = new ArrayList<>();
        for (Category category : categoryList) {
            categoryVoList.add(copy(category));
        }
        return categoryVoList;
    }

    private CategoryVo copy(Category category) {

        CategoryVo categoryVo = new CategoryVo();

        BeanUtils.copyProperties(category,categoryVo);
        categoryVo.setId(String.valueOf(category.getId()));
        return categoryVo;
    }
}
